package com.uenpay.im.ui.old.activity.group;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.qyx.android.entity.GroupTalkEntity;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-20 下午2:58:32
 * @copyRight http://www.qiyunxin.com
 * @function 搜索群聊结果
 */
public class SearchGroupResultAdapter extends BaseAdapter {
	LayoutInflater inflater;
	private ArrayList<GroupTalkEntity> groups;

	public SearchGroupResultAdapter(Context mContext,
			ArrayList<GroupTalkEntity> _groups) {
		inflater = LayoutInflater.from(mContext);
		groups = _groups;
	}

	public void addData(ArrayList<GroupTalkEntity> _groups) {
		groups.addAll(_groups);
		notifyDataSetChanged();
	}

	public void setData(ArrayList<GroupTalkEntity> _groups) {
		groups = _groups;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return groups == null ? 0 : groups.size();
	}

	@Override
	public Object getItem(int arg0) {
		return groups == null ? 0 : groups.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return groups == null ? 0 : arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final MyGroupsViewHolder holder;
		GroupTalkEntity gt = groups.get(position);
		if (convertView == null) {
			holder = new MyGroupsViewHolder();
			convertView = inflater.inflate(R.layout.search_groups_list_item,
					parent, false);
			holder.iv = (RoundImageView) convertView
					.findViewById(R.id.my_groups_avatar);
			holder.name = (TextView) convertView
					.findViewById(R.id.my_groups_name);
			holder.size = (TextView) convertView.findViewById(R.id.group_size);
			holder.joinStatus = (TextView) convertView
					.findViewById(R.id.join_status);
			convertView.setTag(holder);
		} else {
			holder = (MyGroupsViewHolder) convertView.getTag();
		}

		holder.name.setText(gt.group_name);
		holder.iv.SetUrl(APIConfiguration.getAvatarUrlNormal(gt.group_id + "",
				PacketType.GROUP));
		holder.size.setText(gt.has_count + "/" + gt.max_count);
		if (gt.is_admin == 1) {
			holder.joinStatus.setVisibility(View.VISIBLE);
		} else {
			holder.joinStatus.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}

	class MyGroupsViewHolder {
		private RoundImageView iv;
		private TextView name, size, joinStatus;
	}

}
